package com.lanou3g.ptp.organize.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lanou3g.ptp.entity.Clazz;
import com.lanou3g.ptp.entity.College;
import com.lanou3g.ptp.entity.Major;
import com.lanou3g.ptp.entity.User;
import com.lanou3g.ptp.organize.mapper.MajorMapper;
import com.lanou3g.ptp.organize.service.ClazzService;
import com.lanou3g.ptp.organize.service.CollegeService;
import com.lanou3g.ptp.organize.service.MajorService;
import com.lanou3g.ptp.user.service.UserService;
import com.lanou3g.ptp.vo.Option;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.Date;
import java.util.List;

@Service(timeout = 10000)
public class MajorServiceImpl implements MajorService {
    @Autowired
    private MajorMapper majorMapper;
    @Autowired
    private CollegeService collegeService;
    @Autowired
    private ClazzService clazzService;
    @Reference
    private UserService userService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Value("${REDIS_MAJOR_KEY_PREFIX}")
    private String REDIS_MAJOR_KEY_PREFIX;

    @Value("${REDIS_MAJOR_KEY_EXPIRE}")
    private Long REDIS_MAJOR_KEY_EXPIRE;

    @Override
    public void addMajor(Major major) {
        Date now = new Date();
        major.setCreateTime(now);
        major.setUpdateTime(now);
        major.setStatus(0);
        majorMapper.addMajor(major);
        College college = collegeService.findById(major.getCollegeId());
        collegeService.cacheColleges(college.getSchoolId());
    }

    @Override
    public Major findById(Long id) {
        return majorMapper.findById(id);
    }

    @Override
    public List<Major> findByIds(Long[] ids) {
        return majorMapper.findByIds(ids);
    }

    @Override
    public Major findByName(String name) {
        return majorMapper.findByName(name);
    }

    @Override
    public Major findByName(Long collegeId, String name) {
        return majorMapper.findByCollegeIdAndName(collegeId, name);
    }

    @Override
    public List<Major> findAll(Integer page, Integer size) {
        Page<Major> pages = PageHelper.startPage(page, size);
        List<Major> majors = majorMapper.findAll();
        return pages;
    }

    @Override
    public void updateById(Major major) {
        major.setUpdateTime(new Date());
        majorMapper.updateById(major);
        College college = collegeService.findById(major.getCollegeId());
        collegeService.cacheColleges(college.getSchoolId());
    }

    @Override
    public void deleteById(Long id) {
        Major major = findById(id);
        majorMapper.deleteById(id);
        College college = collegeService.findById(major.getCollegeId());
        collegeService.cacheColleges(college.getSchoolId());
    }

    @Override
    public void deleteByIds(Long[] ids) {
        majorMapper.deleteByIds(ids);
    }

    @Override
    public List<Major> findByCollegeId(Long collegeId) {
        List<Major> majors = majorMapper.findByCollegeId(collegeId);
        College college = collegeService.findById(collegeId);
        for (Major major : majors) {
            Long leaderId = major.getLeaderId();
            if (leaderId != null) {
                User leader = userService.userById(leaderId);
                major.setLeaderName(leader.getName());
            }
            List<Clazz> clazzes = clazzService.findByMajorId(major.getId());
            major.setChildren(clazzes);
            major.setParentNode(college.getName());
            major.setParentId(collegeId);
            major.setLevel(2);
        }
        return majors;
    }

    @Override
    public List<Major> findByCollegeIdSimple(Long collegeId) {
        return majorMapper.findByCollegeId(collegeId);
    }

    @Override
    public String[] findNamesByCollegeId(Long collegeId) {
        List<Major> majors = majorMapper.findByCollegeId(collegeId);
        int size = majors.size();
        String[] majorNames = new String[size];
        for (int i = 0; i < size; i++) {
            majorNames[i] = majors.get(i).getName();
        }
        return majorNames;
    }

    @Override
    public List<Option> options() {
        return this.majorMapper.options();
    }
}
